// pages/editpage/index.js
const {
  fetch
} = require('../../utils/fetch')

Page({
  /**
   * 页面的初始数据
   */

  data: {
    value: '',
    activeUrl: 'audit',
    active: 0,
    specificarea: null,
    myhotlist: [],
    swiperImglist: [],
    infoword: '',
    adword: '',
    hasMore: 0,//正在加载
    num:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    if (wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea: wx.getStorageSync('specificarea')
      })
    }

    // 我的信息置顶付费列表
    this.messagetoplist()
   
  },

  messagetoplist() {
    fetch(`wx/message/${this.data.specificarea}/mytoplist`, 'POST', {
      memberId: wx.getStorageSync('userInfo').id,
      keyword: this.data.infoword,
      pageSize:10,
      pageNumber:this.data.num
    }).then(res => {
      if (res) {
        this.setData({
          myhotlist:this.data.myhotlist.concat(res.list)
        })

        this.stopRefreshFn({
          myhotlist: this.data.myhotlist,
          pageCount:res.pageCount
      })
      }
    })
  },
  adtoplist() {
    fetch(`wx/ad/${this.data.specificarea}/mytoplist`, 'POST', {
      memberId: wx.getStorageSync('userInfo').id,
      keyword: this.data.adword
    }).then(res => {
      this.setData({
        swiperImglist: this.data.swiperImglist.concat(res.list)
      })
      this.stopRefreshFn({
        swiperImglist: this.data.swiperImglist,
        pageCount:res.pageCount
    })
    })
  },
  goinfoValue(e) {
    this.setData({
      infoword:e.detail.value
    })
  },
  goinfoSearch() {
    this.setData({
      myhotlist:[],
      num:1
    })
    this.messagetoplist()
  },
  goadValue(e) {
    this.setData({
      adword:e.detail.value
    })
  },
  goadSearch() {
    this.setData({
      swiperImglist:[],
      num:1
    })
    console.log(this.data.adword, '广告')
    this.adtoplist()
  },
  onChange(event) {
    //  搜索 event.detail 为当前输入的值
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      active:event.detail.index
    })
    if (event.detail.index === 0) {
      this.setData({
        myhotlist:[],
        num:1
      })
      this.messagetoplist()
    }
    if (event.detail.index === 1) {
      this.setData({
        swiperImglist:[],
        num:1
      })
       // 广告信息置顶
       this.adtoplist()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.hasMore) {
      return;
    }
    if(this.data.active == 0) {
      this.setData({
        num: this.data.num+1,
    })
    this.messagetoplist()
    } else {
      this.setData({
        num: this.data.num+1,
    })
    this.adtoplist()
    }
    
  },


  
  stopRefreshFn(obj) {
    if(obj.pageCount == 0) {
      this.setData({
        hasMore:false
      })
      return;
    }
    if(obj.pageCount == this.data.num) {
      this.setData({
        hasMore:false
      })
      return;
    } 
      this.setData({
        hasMore:true
      })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})